﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace CNFProblem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string _example =
@"a b c
b c ~a
a ~b c
~a ~b c";

        public MainWindow()
        {
            InitializeComponent();
            //cnf.Text = _example;
        }

        private CNF.LogicalFormula3CNF ParseFormula()
        {
            try
            {
                string str = _buildLiteral.Literal;
                return CNF.Parser.Parse(str);
                //return CNF.Parser.Parse(cnf.Text);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        private void parse_Click(object sender, RoutedEventArgs e)
        {
            /*var lf = ParseFormula();
            if (lf != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in lf)
                {
                    sb.Append(item.ToString() + "\n");
                }
                MessageBox.Show(sb.ToString());
            }*/
            MessageBox.Show(_buildLiteral.Literal);
        }

        private void compute_Click(object sender, RoutedEventArgs e)
        {
            var lf = ParseFormula();
            if (lf != null)
            {
                CNF.IAlgorithm algo = null;

                if (_m1.IsChecked.Value)
                    algo = new CNF.NaiveAlgorithm();
                else
                    if (_m2.IsChecked.Value)
                        algo = new CNF.Algo183();
                    else
                        if (_m3.IsChecked.Value)
                            algo = new CNF.Algo176();

                Dictionary<int, bool> values;
                if (algo.Compute(lf, out values))
                {
                    StringBuilder sb = new StringBuilder();
                    var dict = lf.Variables.IdToVariable();
                    foreach (var item in values)
                    {
                        sb.Append(dict[item.Key].Name + ":" + item.Value + "\n");
                    }
                    MessageBox.Show("TAK\n" + sb.ToString());
                }
                else
                    MessageBox.Show("NIE");

            }
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents with formula (.txt)|*.txt";
            if (dlg.ShowDialog() == true)
            {
                // Save document
                using (StreamWriter outfile = new StreamWriter(dlg.FileName))
                {
                    outfile.Write(_buildLiteral.Literal);
                }
            }
        }

        private void loadButton_Click(object sender, RoutedEventArgs e)
        {
            // Configure save file dialog box
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.Filter = "Text documents with formula (.txt)|*.txt"; // Filter files by extension

            if (dlg.ShowDialog() == true)
            {
                try
                {
                    string str = System.IO.File.ReadAllText(dlg.FileName);
                    CNF.LogicalFormula3CNF formula = CNF.Parser.Parse(str);
                    _buildLiteral.BuildFromFormula(formula);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
